#ifndef HOVERBUTTON_H
#define HOVERBUTTON_H

#include <QObject>
#include <QPushButton>
#include <QEvent>
#include <QPoint>
#include <QPropertyAnimation>

#include "MonitorCard.h"

class HoverButton : public QPushButton
{
    Q_OBJECT
public:
    HoverButton();

    explicit HoverButton(const QString &text, QWidget *parent = nullptr)
        : QPushButton(text, parent)
    {
        initCard();
    }

    explicit HoverButton(QWidget *parent = nullptr) : QPushButton(parent)
    {
        m_card = new MonitorCard(this);
        m_card->hide();
    }
protected:
    // 鼠标进入按钮时显示卡片
    void enterEvent(QEnterEvent  *event) override
    {
        QPushButton::enterEvent(event); // 调用父类方法，确保正常事件传递
        showCard();
    }

    // 鼠标离开按钮时隐藏卡片
    void leaveEvent(QEvent *event) override
    {
        QPushButton::leaveEvent(event);

        // 创建并启动卡片的消失动画
        QPropertyAnimation *fadeOutOpacity = new QPropertyAnimation(m_card, "windowOpacity");
        fadeOutOpacity->setDuration(400);
        fadeOutOpacity->setStartValue(1);  // 初始透明度
        fadeOutOpacity->setEndValue(0);    // 最终透明度
        fadeOutOpacity->setEasingCurve(QEasingCurve::InOutQuad);

        QPropertyAnimation *fadeOutSize = new QPropertyAnimation(m_card, "geometry");
        fadeOutSize->setDuration(400);
        fadeOutSize->setStartValue(m_card->geometry());  // 当前几何形状
        fadeOutSize->setEndValue(QRect(m_card->pos(), QSize(0, 0)));  // 最终目标：缩小到 (0, 0)

        connect(fadeOutOpacity, &QPropertyAnimation::finished, m_card, &QWidget::hide); // 动画结束后隐藏卡片

        m_card->adjustSize();
        m_card->update();  // 强制重新绘制父窗口

        for (const auto& child : this->findChildren<QWidget*>()) {
            child->setMinimumSize(0, 0);
            child->setVisible(true);
        }

        fadeOutOpacity->start();
        fadeOutSize->start();
        qDebug() << "Card Size:" << m_card->size() << "Visible:" << m_card->isVisible();

    }

private:
    MonitorCard *m_card;

    // 计算卡片显示位置并显示
    void showCard() {
        QPoint buttonPos = this->mapToGlobal(QPoint(0, 0)); // 按钮左上角的全局坐标
        int x = buttonPos.x() + this->width() + 10;
        int y = buttonPos.y() - 10;
        m_card->move(x, y);

        m_card->setGeometry(x, y, 400, 400);

        // 设置卡片的初始透明度为 0 和初始尺寸为 (0, 0)
        m_card->setWindowOpacity(0);
        m_card->resize(0, 0);  // 初始尺寸为 (0, 0)
        m_card->setMinimumSize(0, 0); // 设置最小尺寸为 (0, 0)
        m_card->show();

        // 创建并启动卡片的显示动画：从小到大，透明度从 0 到 1
        QPropertyAnimation *fadeInOpacity = new QPropertyAnimation(m_card, "windowOpacity");
        fadeInOpacity->setDuration(400);
        fadeInOpacity->setStartValue(0); // 初始透明度
        fadeInOpacity->setEndValue(1);   // 最终透明度
        fadeInOpacity->setEasingCurve(QEasingCurve::InOutQuad);

        QPropertyAnimation *fadeInSize = new QPropertyAnimation(m_card, "geometry");
        fadeInSize->setDuration(400);
        fadeInSize->setStartValue(QRect(m_card->pos(), QSize(0, 0))); // 初始大小为 (0, 0)
        fadeInSize->setEndValue(QRect(m_card->pos(), QSize(400, 400))); // 最终恢复到正常大小

        // 显示子组件

        m_card->adjustSize();
        m_card->update();  // 强制重新绘制父窗口

        for (const auto& child : this->findChildren<QWidget*>())
        {
            child->setVisible(true);
        }

        fadeInOpacity->start();
        fadeInSize->start();
    }






    // 提取卡片初始化代码，避免重复
    void initCard()
    {
        m_card = new MonitorCard(this);
        m_card->hide();
    }

};

#endif // HOVERBUTTON_H
